package ma.ensao.egicom.dao.implementations;
 
import java.util.List;

import ma.ensao.egicom.core.model.Customer;
import ma.ensao.egicom.dao.interfaces.CustomerDAO;

import org.hibernate.Query; 
import org.scub.foundation.framework.core.dao.implementations.HibernateDaoBaseImplementation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomerDAOImpl extends HibernateDaoBaseImplementation implements CustomerDAO {

    
    private static final Logger log = LoggerFactory.getLogger(CustomerDAO.class);
    // property constants
    public static final String ID = "id";
    public static final String FIRSTNAME = "firstname";
    public static final String LASTNAME = "lastname";
    public static final String EMAIL = "email";
    public static final String PASSWORD = "password";
    public static final String ADR = "adr";
    public static final String CITY = "city";

    /**
     * {@inheritDoc}
     */
    @Override
    public void save(Customer transientInstance) { 
        save(transientInstance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void delete(Customer persistentInstance) { 
        delete(persistentInstance);
         
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Customer findById(java.lang.Integer id) {
        return load(id, Customer.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Customer instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from Customer as model where model."
                    + propertyName + "= ?";
            Query queryObject = createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findById(Object id) {
        return findByProperty(ID, id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByFirstname(Object firstname) {
        return findByProperty(FIRSTNAME, firstname);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByLastname(Object lastname) {
        return findByProperty(LASTNAME, lastname);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByEmail(Object email) {
        return findByProperty(EMAIL, email);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByPassword(Object password) {
        return findByProperty(PASSWORD, password);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByAdr(Object adr) {
        return findByProperty(ADR, adr);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findByCity(Object city) {
        return findByProperty(CITY, city);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List findAll() {
        return loadAll(Customer.class);
    }
 
}